<template>
  <div>
    <dl class="movie-list">
      <div v-if="MovieLen === 0" class="mo-movies">
        抱歉，没有找到相关结果，请尝试用其他条件筛选。
      </div>
      <dd v-for="(movie, item) in MovieList" :key="movie.id">
        <div class="movie-item film-channel">
          <div class="movie-poster">
            <img :src="movie.mImage" :alt="movie.mName" />
          </div>
          <div class="movie-ver"></div>
          <div class="movie-item-hover">
            <a
              href="#"
              @click="toDetail(movie.id)"
            >
              <img
                class="movie-hover-img"
                :src="movie.mImage"
                :alt="movie.mName"
              />
              <div class="movie-hover-info">
                <div class="movie-hover-title" :title="movie.mName">
                  <span class="name noscore">{{ movie.mName }}</span>
                </div>
                <div class="movie-hover-title" :title="movie.mName">
                  <span class="hover-tag">类型:</span>
                  {{ movie.mType }}
                </div>
                <div class="movie-hover-title" :title="movie.mName">
                  <span class="hover-tag">主演:</span>
                  {{ movie.mActor }}
                </div>
                <div
                  class="movie-hover-title movie-hover-brief"
                  title="这么多年"
                >
                  <span class="hover-tag">上映时间:</span>
                  {{ movie.mDate | mdate }}
                </div>
              </div>
            </a>
          </div>
        </div>
        <div
          class="channel-detail movie-item-title"
        >
          {{ movie.mName }}
        </div>
        <div class="channel-detail channel-detail-orange">
          <i v-if="movie.mScore !== 0" class="integer"
            >{{ integer(movie.mScore) }}.</i
          >
          <i v-if="movie.mScore !== 0" class="fraction">{{
            fraction(movie.mScore)
          }}</i>
          <span v-if="movie.mScore === 0">暂无评分</span>
        </div>
      </dd>
    </dl>
    <div style="text-align: center">
      <el-pagination
        background
        :page-size="limit"
        layout="prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :total="MovieLen"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import dayjs from 'dayjs'
export default {
  name: 'VueIndex',

  props: ['index'],

  data () {
    return {
      MovieList: [],
      page: 1, // 默认第一页
      limit: 18, // 每页显示的条数
      type: 0,
      MovieLen: 0
    }
  },
  computed: {

  },
  filters: {
    mdate (value) {
      return dayjs(value).format('YYYY-MM-DD')
    }
  },

  mounted () {
    this.getMovieList()
    console.log(this.$route.query)
    this.$bus.$on('getType', (res) => {
      this.type = res
    })
  },
  watch: {
    type: {
      handler (newValue, oldValue) {
        this.getMovieList()
      }
    }
  },

  methods: {
    toDetail (id) {
      // 路由传递参数
      this.$router.push({
        name: 'toDetail',
        params: { id: id }
      })
    },
    getMovieList () {
      axios({
        url: '/api/movie/GetMovieByType',
        method: 'get',
        params: {
          page: this.page,
          limit: this.limit,
          type: this.type,
          status: this.$route.query.showType ? this.$route.query.showType : 1
        }
      }).then((res) => {
        console.log(res)
        this.MovieLen = res.data.data.total
        const data = res.data.data.list
        data.forEach((item) => {
          item.mType = item.mType.map(obj => obj.name).join(' / ')
          item.mActor = item.mActor.replace(/、/g, ' / ')
        })
        this.MovieList = data
      })
    },
    getScore (score) {
      return score === 0 ? '暂无评分' : (parseInt(score * 10) / 10) * 2
    },
    // 小数点之前的数字
    integer (score) {
      return parseInt(this.getScore(score))
    },
    // 小数点之后的数字
    fraction (score) {
      return parseInt((this.getScore(score) - this.integer(score)) * 10)
    },
    handleSizeChange (val) {
      this.limit = val
      this.getMovieList()
    },
    handleCurrentChange (val) {
      this.page = val
      this.getMovieList()
    }
  }
}
</script>

  <style lang="scss" scoped>
.movie-list {
  margin: -29px 0 20px -25px;
}
.movie-list dd {
  margin: 30px 0 0 30px;
  display: inline-block;
  vertical-align: top;
  position: relative;
}
.movie-item {
  position: relative;
  border: 1px solid #efefef;
  margin: -1px;
  cursor: pointer;
}
.movie-poster {
  background-color: #fcfcfc;
  width: 160px;
  height: 220px;
  overflow: hidden;
  position: relative;
}
.movie-poster img {
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
.channel-detail {
  width: 160px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
  font-size: 16px;
  color: #333;
  margin-top: 10px;
}

.movie-item-title {
  cursor: pointer;
}

.channel-detail {
  width: 160px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
  font-size: 16px;
  color: #333;
  margin-top: 10px;
}
.channel-detail-orange,
.channel-detail-prescore {
  color: #ffb400;
}
.channel-detail-orange .integer {
  font-size: 24px;
}
.channel-detail-orange .fraction {
  font-size: 22px;
}
.no-movies {
  margin-top: 40px;
  font-size: 16px;
  color: #333;
}
.movie-ver {
  position: absolute;
  top: 4px;
  left: -2px;
  font-size: 12px;
  color: #fff;
}
.movie-item:hover .movie-item-hover {
  display: block;
}
img.movie-hover-img {
    width: 218px;
    height: 300px;
    vertical-align: middle;
    position: absolute;
    z-index: 10;
}
.movie-hover-info {
    padding: 16px;
    height: 150px;
    box-sizing: border-box;
    position: absolute;
    z-index: 11;
    top: 150px;
    background: #fff;
    width: 218px;
}
.movie-hover-title:first-child {
    margin-top: 6px;
    font-size: 20px;
    color: #333;
    line-height: 20px;
}
.movie-hover-title:last-child {
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    line-height: 20px;
    margin-top: 10px;
}
.movie-item-hover {
    display: none;
    position: absolute;
    width: 218px;
    height: 300px;
    z-index: 10;
    top: -40px;
    left: -29px;
    overflow: hidden;
    background: #fff;
    -moz-box-shadow: 0 0 16px #fff,0 0 6px rgba(0,0,0,.2);
    -webkit-box-shadow: 0 0 16px #fff, 0 0 6px rgba(0,0,0,.2);
    box-shadow: 0 0 16px #fff, 0 0 6px rgba(0,0,0,.2);
}
.movie-hover-title {
    font-size: 16px;
    line-height: 16px;
    color: #999;
    margin-top: 12px;
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.movie-item-hover .noscore {
    width: 100%;
}
.movie-item-hover .name {
    float: left;
    width: 140px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.hover-tag {
    color: #333;
}
</style>
